﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using AccountingLibs;

namespace Lists
{
    public class Store : DoiTuong
    {
        #region StoreID property
        public string StoreID
        {
            get
            {
                return (string)_DataRow["StoreID"];
            }
            set
            {
                _DataRow["StoreID"] = value;
            }
        }
        #endregion

        #region Name property
        public string Ten
        {
            get
            {
                return (string)_DataRow["Name"];
            }
            set
            {
                _DataRow["Name"] = value;
            }
        }
        #endregion

        public Store(DataRow dataRow)
            : base(dataRow)
        {
        }

        public class DanhMucStore : DanhMucDoiTuong, IEnumerable
        {
            #region Instance property: Singleton Design pattern
            private static DanhMucStore _Instance;
            public static DanhMucStore Instance
            {
                get
                {
                    if (_Instance == null)
                    {
                        _Instance = new DanhMucStore();
                        UniqueConstraint uniqueConstraint = new UniqueConstraint(_Instance.DataTable.Columns["StoreID"]);
                        _Instance.DataTable.Constraints.Add(uniqueConstraint);
                        _Instance.DataView = _Instance.DataTable.DefaultView;
                        _Instance.DataView.Sort = "StoreID";
                    }
                    return _Instance;
                }
            }
            #endregion

            #region override TableName property
            public override string TableName
            {
                get
                {
                    return "Store";
                }
            }
            #endregion

            #region Methods required by IEnumerable (use for foreach)
            public IEnumerator GetEnumerator()
            {
                return new TokenEnumerator(this);
            }

            private class TokenEnumerator : IEnumerator
            {
                private int _Position = -1;
                DanhMucDoiTuong _Dstk;

                public TokenEnumerator(DanhMucDoiTuong dstk)
                {
                    _Dstk = dstk;
                }

                // Declare the MoveNext method required by IEnumerator:
                public bool MoveNext()
                {
                    if (_Position < _Dstk.DataView.Count - 1)
                    {
                        _Position++;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }

                // Declare the Reset method required by IEnumerator:
                public void Reset()
                {
                    _Position = -1;
                }

                // Declare the Current property required by IEnumerator:
                public object Current
                {
                    get
                    {
                        return new Store((_Dstk.DataView[_Position].Row));
                    }
                }
            }
            #endregion

            public Store this[int index]
            {
                get
                {
                    if (0 <= index && index <= this.Count)
                    {
                        return new Store(DataTable.Rows[index]);
                    }
                    else
                    {
                        return null;
                    }
                }
            }

            public Store NewDoiTuong()
            {
                return new Store(DataTable.NewRow());
            }

            public Store GetByProperty(string property, object value)
            {
                try
                {
                    DataRow[] rows = GetDataRows(property, value);
                    if (rows.Length > 0) return new Store(rows[0]);
                    else return null;
                }
                catch (Exception ex)
                {
                    if (ex is CustomException) throw ex;
                    else
                    {
                        throw new CustomException(ex.Message);
                    }
                }
            }

            public Store GetByID(Guid objID)
            {
                return GetByProperty("ID", objID);
            }

            public static DanhMucStore GetAll()
            {
                return Instance;
            }

            public static DanhMucStore Filter(string filterExpression)
            {
                try
                {
                    DanhMucStore obj = new DanhMucStore();
                    obj.DataView = new DataView(DanhMucStore.Instance.DataTable, filterExpression, "StoreID", DataViewRowState.CurrentRows);
                    return obj;
                }
                catch (SqlException sqlEx)
                {
                    throw new CustomException(CustomException.GetSqlExceptionMessage(sqlEx));
                }
                catch (Exception ex)
                {
                    if (ex is CustomException) throw ex;
                    else
                    {
                        throw new CustomException(ex.Message);
                    }
                }
            }

        }
    }
}
